package com.tianqu.tms.www.tradeStatistics.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.www.tradeStatistics.action.SpecialCarListTopAction;
import com.tianqu.tms.www.tradeStatistics.actionform.SpecialCarForm;

/**
 * @author zhangxueliang
 * 特殊车辆处理列表controller
 */
public class SpecialCarListTopController extends TmsController{
	private Logger logger = Logger.getLogger(SpecialCarListTopController.class);
	public static final String PAGING = "paging";
	public static final String SCARLIST = "sCarList";
	@Override
	public void execute(UserContext uc) throws Exception {
		TxController controller = TxController.getTxController();
		// 添加事务提交异常捕获
		try {
			controller.begin();
			SpecialCarForm carForm = (SpecialCarForm)uc.getInputForm();
			Car car = Car.getInstance();

			if (uc.getAttribute(SpecialCarListTopAction.PAGENO) != null) {
				car.setPageNo(Integer.parseInt((String) uc
						.getAttribute(SpecialCarListTopAction.PAGENO)));
			}
			if (uc.getAttribute(SpecialCarListTopAction.MAXINDEX) != null) {
				car.setMaxIndex(Integer.parseInt((String) uc
						.getAttribute(SpecialCarListTopAction.MAXINDEX)));
			}
			if (uc.getAttribute(SpecialCarListTopAction.SORTKEY) != null) {
				car.setSortKey(Integer.parseInt((String) uc
						.getAttribute(SpecialCarListTopAction.SORTKEY)));
			}
			if (uc.getAttribute(SpecialCarListTopAction.ASC) != null) {
				car.setAsc((new Boolean((String) uc
						.getAttribute(SpecialCarListTopAction.ASC)))
						.booleanValue());
			}
			
			List list = new ArrayList();	
			List list1 = new ArrayList();
			if (uc.getAttribute("carForm") == null) {
				// 判定翻页
				list = car.findSpecialCar(carForm.getStartTime(),carForm.getEndTime(),carForm.getCarFrom(),carForm.getCarRidio());
				list1 = car.findAllSpecialCar(carForm.getStartTime(),carForm.getEndTime(),carForm.getCarFrom(),carForm.getCarRidio());
			} else {
				carForm = (SpecialCarForm) uc
						.getAttribute("carForm");
				list = car.findSpecialCar(carForm.getStartTime(),carForm.getEndTime(),carForm.getCarFrom(),carForm.getCarRidio());
				list1 = car.findAllSpecialCar(carForm.getStartTime(),carForm.getEndTime(),carForm.getCarFrom(),carForm.getCarRidio());
			}

			uc.setAttribute(TmsConstants.VIEWHELP, list);
			uc.setAttribute(SCARLIST, list1);
			uc.setAttribute(PAGING, car);
			uc.setAttribute("carForm", carForm);
			
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
		
	}

}
